package com.song.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.song.common.model.PageModel;
import com.song.common.model.UserModel;
import com.song.common.util.JwtUtil;
import com.song.user.util.RedisUtil;
import com.song.user.view.po.LoginParam;
import com.song.user.view.po.UserPO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import com.song.user.entity.UserInfo;
import com.song.user.service.UserInfoService;
import com.song.common.result.ResultEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.Serializable;
/**
* <p>
    * 用户信息 控制器
    * </p>
*
* @author song
* @since 2024-06-05
*/
@Api(tags = {"用户信息 控制器"})
@RestController
@RequestMapping("/user_info")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ObjectMapper objectMapper;

    /**
    * 分页查询数据
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "按条件分页检索用户信息",notes = "按条件分页检索用户信息")
    @GetMapping(value = "/pageList")
    public ResultEntity<PageModel<UserModel>> pageList(
        @Validated UserInfo userInfo,
        @ApiParam(value = "页码", defaultValue = "1")
        @RequestParam(defaultValue = "1") int pageNum,
        @ApiParam(value = "分页大小", defaultValue = "10")
        @RequestParam(defaultValue = "10") int pageSize
    ) {
        return ResultEntity.success(userInfoService.pageList(pageNum, pageSize,userInfo));
    }

    /**
    * 用户信息新增
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "新增用户信息",notes = "新增用户信息")
    @PostMapping(value = "/add")
    public ResultEntity add(@RequestBody UserPO userPO) {
        UserInfo dbUserByUsername = userInfoService.getOne(new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getUsername, userPO.getUsername()));
        if (dbUserByUsername != null){
            return ResultEntity.failMessage("用户名已存在");
        }
        UserInfo dbUserByIdCard = userInfoService.getOne(new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getIdCard, userPO.getIdCard()));
        if (dbUserByIdCard != null){
            return ResultEntity.failMessage("身份证号已存在");
        }
        userInfoService.addUser(userPO);
        return ResultEntity.success();
    }

    /**
    * 用户信息修改
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "修改用户信息",notes = "修改用户信息")
    @PostMapping(value = "/update")
    public ResultEntity update(@RequestBody UserPO userPO) {
        userInfoService.updateUser(userPO);
        return ResultEntity.success();
    }

    /**
    * 删除用户信息
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "按id删除用户信息",notes = "按id删除用户信息")
    @PostMapping(value = "/delete")
    public ResultEntity delete(@RequestParam String id) {
        userInfoService.deleteUserInfoById(id);
        return ResultEntity.success();
    }


    /**
    * 根据id查询用户信息
    *
    * @author song
    * @Date 2024-06-05
    */
    @ApiOperation(value = "根据id查询用户信息",notes = "根据id查询修改用户信息")
    @GetMapping(value = "/detail")
    public ResultEntity<UserInfo> detail(@RequestParam String id) {
        UserModel userModel = userInfoService.detailById(id);
        return ResultEntity.success(userModel);
    }

    @ApiOperation(value = "用户登录",notes = "用户登录")
    @PostMapping(value = "/login")
    public ResultEntity login(@RequestBody LoginParam loginParam){
        if (StringUtils.isEmpty(loginParam.getUsername()) || StringUtils.isEmpty(loginParam.getPassword())){
            return ResultEntity.failMessage("用户名或密码不能为空");
        }
        UserInfo dbUserByUsername = userInfoService.getOne(new QueryWrapper<UserInfo>().lambda().eq(UserInfo::getUsername, loginParam.getUsername()));
        if (dbUserByUsername == null){
            return ResultEntity.failMessage("用户名不存在");
        }
        if (!loginParam.getPassword().equals(dbUserByUsername.getPassword())){
            return ResultEntity.failMessage("密码错误");
        }
        String token = JwtUtil.sign(loginParam.getUsername());
        try {
            redisUtil.hashSet("login_user", loginParam.getUsername(), objectMapper.writeValueAsString(dbUserByUsername));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return ResultEntity.success(token);
    }

    @ApiOperation(value = "用户登出",notes = "用户登出")
    @PostMapping(value = "/logout")
    public ResultEntity logout(@RequestBody LoginParam loginParam){
        UserInfo userInfo = redisUtil.hashGet("login_user", loginParam.getUsername(), UserInfo.class);
        if (userInfo != null){
            redisUtil.hashDel("login_user", loginParam.getUsername());
        }
        return ResultEntity.success();
    }
}